package com.bluefox.hospital.convertor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bluefox.common.web.dto.AbstractConvertor;
import com.bluefox.hospital.dto.VideoDTO;
import com.bluefox.hospital.model.Video;
import com.bluefox.hospital.service.VideoService;
import lombok.NonNull;

/**
 * VideoConvertor
 */
@Component
public class VideoConvertor extends AbstractConvertor<Video, VideoDTO> {

    @Autowired
    private VideoService videoService;

    @Override
    public Video toModel(@NonNull final VideoDTO dto) {
        if (dto.isNew()) {//新增
            return constructModel(dto);
        } else {//更新
            return updateModel(dto);
        }
    }

    @Override
    public VideoDTO toDTO(@NonNull final Video model, final boolean forListView) {
        final VideoDTO dto = new VideoDTO();
        dto.setId(model.getId());
        dto.setTitle(model.getTitle());
        dto.setUrl(model.getUrl());
        dto.setCreatedBy(model.getCreatedBy().get());
        dto.setVisitors(model.getVisitors());
        dto.setCreatedDate(model.getCreatedDate().get());
        return dto;
    }

    // 构建新Model
    private Video constructModel(final VideoDTO dto) {
        Video model = new Video();
        model.setTitle(dto.getTitle());
        model.setUrl(dto.getUrl());
        model.setVisitors(dto.getVisitors());
        return model;
    }

    // 更新Model
    private Video updateModel(final VideoDTO dto) {
        Video model = videoService.get(dto.getId());
        model.setTitle(dto.getTitle());
        model.setUrl(dto.getUrl());
        model.setVisitors(dto.getVisitors());

        return model;
    }
}
